package com.itheima.HomeWork_day09;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/*
比较器比较:
 */
public class UseCompare {
    public static void main(String[] args) {

        //指定排序的规则
//        TreeSet<Teacher> te = new TreeSet<>(new Comparator<Teacher>() {
//            @Override
//            public int compare(Teacher o1, Teacher o2) {
//                int result=o1.getAge()-o2.getAge();
//                 result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
//                return  result;
//            }
//        });


        //lambda函数
        TreeSet<Teacher> te = new TreeSet<Teacher>((o1,o2)->{

            //指定排序规则
                int result=o1.getAge()-o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return  result;

        });




            Teacher t1=new Teacher("mao",35);
            Teacher t2=new Teacher("hu",36);
            Teacher t3=new Teacher("zhao",35);
            Teacher t4=new Teacher("chen",30);

            //添加到集合
        te.add(t1);
        te.add(t2);
        te.add(t3);
        te.add(t4);

        //遍历集合

        //使用迭代器进行遍历
        Iterator<Teacher> it = te.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }


        System.out.println("==============================");
        //使用增强for进行遍历
        for (Teacher teacher : te) {
            System.out.println(teacher);
        }

    }

}
